/*
 * Copyright 2008-2009 Nobject.org, All Rights Reserved.
 * 
 * This software is licensed under LGPL license.
 * See terms of license at gnu.org.
 * 
 * For more information,visit:
 * 
 * http://www.nobject.org
 */
package org.nobject.common.code.model.java;

import java.util.LinkedList;
import java.util.List;

import org.nobject.common.code.model.Constance;

/**
 * Variable Model class
 * 
 * @author bianrongjun
 * @version 1.0
 */
public class VariableModel {

	/** 访问类型 */
	private String variableAccessType = Constance.AccsessType.PUBLIC;
	/** 变量名 */
	private String variableName;
	/** 变量类型 */
	private String variableType;
	/** 变量注释 */
	private String variableComment;
	/** 变量默认值 */
	private String variableDefaultValue;
	/** 变量是否为静态 */
	private boolean variableStatic = false;
	/** 注解 */
	private List<AnnotationModel> annotations=new LinkedList();
	/**
	 * Generate Mode If Generate Set/Get's Method
	 */
	private int generateMode = GENERATE_DISABLED;

	/**
	 * Generate Set's Method
	 */
	public static int GENERATE_SET = 3;

	/**
	 * Generate Get's Method
	 */
	public static int GENERATE_GET = 5;

	/**
	 * Generate Set/Get's Method
	 */
	public static int GENERATE_ALL = GENERATE_SET * GENERATE_GET;

	/**
	 * UnGenerate Set||Get's Method
	 */
	public static int GENERATE_DISABLED = 1;
	
	/** codeIndent */
	private boolean codeIndent=false;
	
	/**
	 * Constructor
	 */
	public VariableModel() {

	}

	/**
	 * Constructor
	 * 
	 * @param variableName
	 * @param variableType
	 * @param variableAccessType
	 */
	public VariableModel(String variableName, String variableType, String variableAccessType) {
		this(variableName, variableType, variableAccessType, false);
	}

	/**
	 * Constructor
	 * 
	 * @param variableName
	 * @param variableType
	 * @param variableAccessType
	 * @param variableStatic
	 */
	public VariableModel(String variableName, String variableType, String variableAccessType, boolean variableStatic) {
		this(variableName, variableType, variableAccessType, variableStatic, GENERATE_DISABLED);
	}

	/**
	 * Constructor
	 * 
	 * @param variableName
	 * @param variableType
	 * @param variableAccessType
	 * @param variableStatic
	 * @param generateMode
	 */
	public VariableModel(String variableName, String variableType, String variableAccessType, boolean variableStatic, int generateMode) {
		this(variableName, variableType, variableAccessType, variableStatic, generateMode, null);
	}

	/**
	 * Constructor
	 * 
	 * @param variableName
	 * @param variableType
	 * @param variableAccessType
	 * @param variableStatic
	 * @param generateMode
	 * @param variableDefaultValue
	 */
	public VariableModel(String variableName, String variableType, String variableAccessType, boolean variableStatic, int generateMode, String variableDefaultValue) {
		this(variableName, variableType, variableAccessType, variableStatic, generateMode, variableDefaultValue, null);
	}

	/**
	 * Constructor
	 * 
	 * @param variableName
	 * @param variableType
	 * @param variableAccessType
	 * @param variableStatic
	 * @param generateMode
	 * @param variableDefaultValue
	 * @param variableComment
	 */
	public VariableModel(String variableName, String variableType, String variableAccessType, boolean variableStatic, int generateMode, String variableDefaultValue, String variableComment) {
		this.variableName = variableName;
		this.variableType = variableType;
		this.variableAccessType = variableAccessType;
		this.variableStatic = variableStatic;
		this.generateMode = generateMode;
		this.variableDefaultValue = variableDefaultValue;
		this.variableComment = variableComment;
	}

	/**
	 * Constructor
	 * 
	 * @param variableName
	 * @param variableType
	 * @param generateMode
	 */
	public VariableModel(String variableName, String variableType, String variableAccessType, int generateMode) {
		this.variableName = variableName;
		this.variableType = variableType;
		this.generateMode = generateMode;
		this.variableAccessType = variableAccessType;
	}

	/**
	 * Constructor
	 * 
	 * @param variableName
	 * @param variableType
	 * @param variableComment
	 * @param generateMode
	 */
	public VariableModel(String variableName, String variableType, String variableAccessType, int generateMode, String variableComment) {
		this.variableName = variableName;
		this.variableType = variableType;
		this.variableComment = variableComment;
		this.generateMode = generateMode;
		this.variableAccessType = variableAccessType;
	}

	/**
	 * Get Variable Access Type
	 * 
	 * @return
	 */
	public String getVariableAccessType() {
		return variableAccessType;
	}

	/**
	 * Set Variable Access Type
	 * 
	 * @param variableAccessType
	 */
	public void setVariableAccessType(String variableAccessType) {
		this.variableAccessType = variableAccessType;
	}

	/**
	 * Set Varaible Method Avalible
	 * 
	 * @param generateMode
	 */
	public void setVaraibleMethodAvalible(int generateMode) {
		this.generateMode = generateMode;
	}

	/**
	 * Set Variable Name
	 * 
	 * @param variableName
	 */
	public void setVariableName(String variableName) {
		this.variableName = variableName;
	}

	/**
	 * Set Variable Type
	 * 
	 * @param variableType
	 */
	public void setVariableType(String variableType) {
		this.variableType = variableType;
	}

	/**
	 * Set Variable Comment
	 * 
	 * @param variableComment
	 */
	public void setVariableComment(String variableComment) {
		this.variableComment = variableComment;
	}

	/**
	 * Get Variable Name
	 * 
	 * @return
	 */
	public String getVariableName() {
		return variableName;
	}

	/**
	 * Get Variable Type
	 * 
	 * @return
	 */
	public String getVariableType() {
		return variableType;
	}

	/**
	 * Get Variable Comment
	 * 
	 * @return
	 */
	public String getVariableComment() {
		return variableComment;
	}

	/** isCodeIndent */
	public boolean isCodeIndent() {
		return codeIndent;
	}

	/** setCodeIndent */
	public void setCodeIndent(boolean codeIndent) {
		this.codeIndent = codeIndent;
	}

	/** getClassAnnnotationClass */
	public List getAnnnotations() {
		return annotations;
	}

	/** setClassAnnnotationClass */
	public void addAnnnotation(AnnotationModel annotationModel) {
		this.annotations.add(annotationModel);
	}
	
	/**
	 * If Get
	 * 
	 * @return
	 */
	public boolean ifGet() {
		return (this.generateMode % GENERATE_GET == 0);
	}

	/**
	 * If Set
	 * 
	 * @return
	 */
	public boolean ifSet() {
		return (this.generateMode % GENERATE_SET == 0);
	}

	/**
	 * If Set&&Get
	 * 
	 * @return
	 */
	public boolean ifSetGet() {
		return (this.generateMode % (GENERATE_GET * GENERATE_SET) == 0);
	}

	/**
	 * Get Variable Default Value
	 * 
	 * @return
	 */
	public String getVariableDefaultValue() {
		return variableDefaultValue;
	}

	/**
	 * Set Variable Default Value
	 * 
	 * @param variableDefaultValue
	 */
	public void setVariableDefaultValue(String variableDefaultValue) {
		this.variableDefaultValue = variableDefaultValue;
	}

	/**
	 * Is Variable Static
	 * 
	 * @return
	 */
	public boolean isVariableStatic() {
		return variableStatic;
	}

	/**
	 * Set Variable Static
	 * 
	 * @param variableStatic
	 */
	public void setVariableStatic(boolean variableStatic) {
		this.variableStatic = variableStatic;
	}

	/**
	 * Check
	 * 
	 * @return
	 */
	public boolean check() {
		if (variableName != null && !variableName.equals("") && variableType != null && !variableType.equals("")) {
			return true;
		} else {
			return false;
		}
	}

	
	
	/**
	 * Generate Variable String
	 */
	public String generateVariableString() {
		StringBuffer sb = new StringBuffer();
		if(this.codeIndent){
			sb.append("\n\t/** ").append(((this.getVariableComment() != null) ? this.getVariableComment() : this.getVariableName())).append(" */\n");
		}else{
			sb.append("\n");
			sb.append("\t/**\n");
			sb.append("\t * " + ((this.getVariableComment() != null) ? this.getVariableComment() : this.getVariableName()) + "\n");
			sb.append("\t */\n");
		}
		
		if(this.annotations.size()>0){
			for(AnnotationModel annotationModel:this.annotations){
				sb.append("\t"+annotationModel.toString()+"\n");
			}
		}
		sb.append("\t" + this.getVariableAccessType() + ((this.isVariableStatic()) ? " static" : "")+ " "  + this.getVariableType()  + " " + this.getVariableName() + ((this.getVariableDefaultValue() != null) ? ("="+(this.getVariableType().equals("String")?"\"":"") + this.getVariableDefaultValue()+(this.getVariableType().equals("String")?"\"":"")) : "") +";");
		return sb.toString();
	}

}
